package com.xxtx.trade.web.noticeController;

import com.alibaba.fastjson.JSON;
import com.xxtx.trade.common.util.SpringUserContext;
import com.xxtx.trade.common.util.model.Result;
import com.xxtx.trade.notice.dto.NoticeDto;
import com.xxtx.trade.notice.service.NoticeService;
import com.xxtx.trade.sysUser.vo.TradeSysUserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1/xxtx/notice")
public class NoticeController {
    private static final Logger LOGGER = LoggerFactory.getLogger(NoticeController.class);

    @Autowired
    private NoticeService noticeService;
    @Autowired
    private SpringUserContext springUserContext;
    /**
     * 查看公告系统（带翻页）
     *
     * @param noticeDto
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public Result<Object> selectNoticeList(@RequestBody NoticeDto noticeDto) {

        Result result = noticeService.selectNoticeList(noticeDto);
        return result;
    }

    /**
     * 删除一条公告
     * 获取其id
     *
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Object> deleteNoticeById(@RequestBody NoticeDto noticeDto) {
        Result<Object> result = noticeService.deleteNoticeById(noticeDto);
        return result;
    }

    /**
     * 增加一条系统公告
     *
     * @param noticeDto
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
   public Result<Object> addNotice(@RequestBody NoticeDto noticeDto) {

        try {
            TradeSysUserVo loginUser = springUserContext.getCurrentLoginUser();
            noticeDto.setNickname(loginUser.getNickName());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result<>(400, "登录异常，请重新登录", "fail", null);
        }
        Result<Object> result1 = noticeService.addNotice(noticeDto);
        return result1;
    }


    /**
     * 查询最新的一条工单
     *
     * @return
     */
    @RequestMapping(value = "/new", method = RequestMethod.POST)
   public Result<Object> newNotice() {
        Result<Object> result = noticeService.newNotice();
        return result;
    }
}
